package com.cssw.fyzb.event.service.video;

import com.cssw.fyzb.core.dao.tesk.RuleInstDao;
import com.cssw.fyzb.core.dao.tesk.TaskExecDao;
import com.cssw.fyzb.core.dao.user.UserExtDao;
import com.cssw.fyzb.core.dao.video.UserVideoDao;
import com.cssw.fyzb.core.dao.video.UserVideoLikeDao;
import com.cssw.fyzb.core.dao.video.VideoViewTimeDao;
import com.cssw.fyzb.core.eao.video.VideoLikeEao;
import com.cssw.fyzb.core.mao.task.TaskStatMao;
import com.cssw.fyzb.core.mao.user.UserMao;
import com.cssw.fyzb.core.mao.video.UserVideoMao;
import com.cssw.fyzb.core.mao.video.VideoStatMao;
import com.cssw.fyzb.core.mao.video.VideoViewStatMao;
import com.cssw.fyzb.core.model.dodb.CmfUserVideo;
import com.cssw.fyzb.core.model.dodb.CmfUserVideoLike;
import com.cssw.fyzb.core.model.dodb.RuleInst;
import com.cssw.fyzb.core.model.dodb.TaskExec;
import com.cssw.fyzb.core.model.mos.TaskStatMo;
import com.cssw.fyzb.core.model.mos.VideoViewStatMo;
import com.cssw.fyzb.core.util.DateUtil;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.simplelock.SimpleLock;
import com.ydn.simplelock.SimpleLockFactory;
import org.apache.lucene.store.SimpleFSLockFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: YangYiQun
 * @Date: 2020/12/22 22:11
 */
@Service
public class VideoServiceImpl implements VideoService {

    private static final Logger logger = LoggerFactory.getLogger(VideoServiceImpl.class);

    @Autowired
    private VideoViewStatMao videoViewStatMao;

    @Autowired
    private VideoLikeEao videoLikeEao;

    @Autowired
    private UserVideoDao usersVideoDao;

    @Autowired
    private UserVideoLikeDao userVideoLikeDao;

    @Autowired
    private VideoStatMao videoStatMao;

    @Autowired
    private UserMao userMao;

    @Autowired
    private VideoViewTimeDao videoViewTimeDao;

    @Autowired
    private UserVideoMao userVideoMao;

    @Autowired
    private SimpleLockFactory simpleLockFactory;

    @Autowired
    private UserExtDao userExtDao;

    @Override
    public void like(Long userId, Long videoId, Long likeTime) {
        SimpleLock videoLock = simpleLockFactory.getLock("LIKE_VIDEO_" + videoId);
        SimpleLock userLock = simpleLockFactory.getLock("LIKE_USER_" + userId);
        try {
            videoLock.acquire();
            userLock.acquire();

            CmfUserVideoLike userVideoLike = userVideoLikeDao.findById(userId, videoId);
            if (null != userVideoLike) {
                return;
            }
            videoStatMao.incLikes(videoId);
            CmfUserVideo cmfUserVideo = usersVideoDao.findById(videoId);
            userVideoLikeDao.add(userId, videoId, new Date(likeTime));
            userVideoMao.like(videoId, userId);
            userExtDao.incLikesNum(userId, 1);
//            videoLikeEao.save(userId, videoId, cmfUserVideo.getUserId(), cmfUserVideo.getTitle(), cmfUserVideo.getHref(),
//                    cmfUserVideo.getThum(), new Date(likeTime));
        } finally {
            try {
                videoLock.release();
            } catch (Exception e) {
                logger.error("", e);
            }
            try {
                userLock.release();
            } catch (Exception e) {
                logger.error("", e);
            }
        }
    }

    @Override
    public void cancelLike(Long userId, Long videoId) {
        SimpleLock videoLock = simpleLockFactory.getLock("LIKE_VIDEO_" + videoId);
        SimpleLock userLock = simpleLockFactory.getLock("LIKE_USER_" + userId);
        try {
            videoLock.acquire();
            userLock.acquire();

            CmfUserVideoLike userVideoLike = userVideoLikeDao.findById(userId, videoId);
            if (null == userVideoLike) {
                return;
            }
            videoStatMao.decLikes(videoId);
            userExtDao.decLikesNum(userId, 1);
            userVideoLikeDao.delete(userId, videoId);
            videoLikeEao.delete(userId, videoId);
            userVideoMao.cancelLike(videoId, userId);
        } finally {
            try {
                videoLock.release();
            } catch (Exception e) {
                logger.error("", e);
            }
            try {
                userLock.release();
            } catch (Exception e) {
                logger.error("", e);
            }
        }
    }

    @Override
    public void viewTimeVideo(Long userId, Long videoId, Integer timeLong) {
        SimpleLock simpleLock = simpleLockFactory.getLock("VIEW_TIME_" + userId);
        try {
            simpleLock.acquire();
            VideoViewStatMo videoViewStatMo = videoViewStatMao.findByUserId(userId);
            String viewDay = DateUtil.formatDate(new Date());
            if (null == videoViewStatMo) {
                videoViewStatMo = newDayMo(userId, viewDay, timeLong);
                videoViewStatMo.setViewTotalTime(timeLong);
                videoViewStatMao.save(videoViewStatMo);
            } else {
                String viewDayMo = videoViewStatMo.getViewDay();
                //如果存的是同一天
                if (viewDay.equals(viewDayMo)) {
                    videoViewStatMo.setViewDayTime(videoViewStatMo.getViewDayTime() + timeLong);
                    videoViewStatMo.setViewTotalTime(videoViewStatMo.getViewTotalTime() + timeLong);
                    videoViewStatMao.update(videoViewStatMo);
                } else {
                    videoViewTimeDao.save(videoViewStatMo.getUserId(), videoViewStatMo.getViewDay(), videoViewStatMo.getViewDayTime());

                    VideoViewStatMo newVideoViewStatMo = newDayMo(userId, viewDay, timeLong);
                    newVideoViewStatMo.setViewTotalTime(videoViewStatMo.getViewTotalTime() + timeLong);
                    videoViewStatMao.update(newVideoViewStatMo);
                }
            }
        } finally {
            try {
                simpleLock.release();
            } catch (Exception e) {
                logger.error("", e);
            }
        }
    }

    @Override
    public void videoAction(Long videoId, String action, Long userId) {
        try {
            if ("1".equals(action)) {
                if (null != userId){
                    userMao.incLikes(userId);
                }
                videoStatMao.incLikes(videoId);
            } else if ("2".equals(action)) {
                if (null != userId){
                    userMao.decLikes(userId);
                }
                videoStatMao.decLikes(videoId);
            } else if ("3".equals(action)) {
                videoStatMao.incComments(videoId);
            } else if ("4".equals(action)) {
                videoStatMao.incShares(videoId);
            } else if ("5".equals(action)) {
                videoStatMao.incViews(videoId);
            }
        } catch (Exception e){
            logger.error("", e);
        }
    }

    /**
     * 新的一天
     *
     * @param userId
     * @param viewDay
     * @param timeLong
     * @return
     */
    private VideoViewStatMo newDayMo(Long userId, String viewDay, Integer timeLong) {
        VideoViewStatMo videoViewStatMo = new VideoViewStatMo();
        videoViewStatMo.setUserId(userId);
        videoViewStatMo.setViewDay(viewDay);
        videoViewStatMo.setViewDayTime(timeLong);

        return videoViewStatMo;
    }
}
